//项目红所有请求由这个文件发出


import jsonp from "jsonp"
import { message } from "antd"
import myAxios from "./myAxios";
import { BASE_ULR, CITY, KEY } from "../config"
//发起登录请求

//登录请求
export const reqLogin = (username, password) => {
    // axios返回的是一个Promise对象
    return myAxios.post(`${BASE_ULR}/login`, { username, password })
}

//获取天气 (【注意】：为了解决跨域问题，用了jsonp库发的请求)
export const reqWeather = () => {
    return new Promise((resolve, reject) => {
        jsonp(`https://restapi.amap.com/v3/weather/weatherInfo?city=${CITY}&key=${KEY}`, (err, data) => {
            if (err) {
                message.error("请求天气接口失败，请联系管理员")
                new Promise(()=>{})  //若失败，中断Promise链，不走catch
            } else {
                const { province, weather, temperature } = data.lives[0]
                let weatherobj = { province, weather, temperature }
                // return weatherobj：【错误】：不能直接才回调里return，这样没法交给jsonp去return
                resolve(weatherobj)
            }
        })
    })
}

//获取商品分类
export const reqCategory = () => {
    return myAxios.get(`${BASE_ULR}/manage/category/list`)
}
//新增商品分类
export const reqAddCategory = (categoryName) => {
    return myAxios.post(`${BASE_ULR}/manage/category/add`,{categoryName})
}
//更新商品分类
export const reqUpdateCategory = (categoryId,categoryName) => {
    return myAxios.post(`${BASE_ULR}/manage/category/update`,{categoryId,categoryName})
}
//获取商品分页列表
export const reqProductList = (pageNum,pageSize) => {
    return myAxios.get(`${BASE_ULR}/manage/product/list`,{params:{pageNum,pageSize}})
}
//更新商品状态
export const reqUpdataProdStatus = (productId,status) => {
    return myAxios.post(`${BASE_ULR}/manage/product/updateStatus`,{productId,status})
}
//搜索商品分页列表
export const reqSearchProdList = (pageNum,pageSize,searchType,keyWord) => {
    return myAxios.get(`${BASE_ULR}/manage/product/search`,{params:{pageNum,pageSize,[searchType]:keyWord}})
}
    /* 以上为简写方法：(第一步：将<Product/>中“按名称/描述搜索”的value改成和后台数据一样的productName/productDesc)
                       (第二步："[]"可以读变量，作为字符串属性)

                       
    if(searchType === "name") {
        return myAxios.get(`${BASE_ULR}/manage/product/search`,{params:{pageNum,pageSize,productName:keyWord}})
    }
    if(searchType === "desc") {
        return myAxios.get(`${BASE_ULR}/manage/product/search`,{params:{pageNum,pageSize,productDesc:keyWord}})
    }
    */
   
//请求商品详情
export const reqProdDetail = (productId) => {
    return myAxios.get(`${BASE_ULR}/manage/product/info`,{params:{productId}})
}

//请求删除图片(根据图片唯一名删除)   
export const reqDeletePicture = (name)=>{
    return myAxios.post(`${BASE_ULR}/manage/img/delete`,{name})
}

//请求添加商品
export const reqAddProduct = (productObj)=>{
    return myAxios.post(`${BASE_ULR}/manage/product/add`,{...productObj})
}

//请求更新商品
export const reqUpdateProduct = (productObj)=>{
    return myAxios.post(`${BASE_ULR}/manage/product/update`,{...productObj})
}

//请求角色分页列表
export const reqRoleList = (pageNum,pageSize)=>{
    return myAxios.get(`${BASE_ULR}/manage/role/list`,{params:{pageNum,pageSize}})
}

//添加角色请求
export const reqAddRole = (roleName)=>{
    return myAxios.post(`${BASE_ULR}/manage/role/add`,{roleName})
}

//请求给角色授权
export const reqAuthRole = (roleObj)=>{
    return myAxios.post(`${BASE_ULR}/manage/role/update`,{...roleObj,auth_time:Date.now()})
}

//请求所有用户(同时携带者角色列表)
export const reqUserList = ()=>{
    return myAxios.get(`${BASE_ULR}/manage/user/list`)
}

//请求添加用户
export const reqAddUser = (userObj)=>{
    return myAxios.post(`${BASE_ULR}/manage/user/add`,{...userObj})
}